/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import java.util.Vector;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author HuuTai_it
 */
public class Vortexs {
    
    private int[] turn = new int[16];
    private int[] disappear = new int[16];
    private boolean[] appearManager = new boolean[4];
    private GameManagement gameMng;
    
    public Vector vVortex = new Vector();
    
    private long time = System.currentTimeMillis();
    
    public Vortexs (GameManagement gameMng, Image image)  throws Exception {
        
        this.gameMng = gameMng;
        for(int i = 0; i < 16; i++) {
            turn[i] = i;
            disappear[i] = 16 + i; 
        }

        gameMng.element[0][0] = 1;
        gameMng.element[0][9] = 1;
        gameMng.element[9][9] = 1;
        gameMng.element[9][0] = 1;
        
        for(int i = 0; i < 4; i++)
            appearManager[i] = true;
        
        Sprite s1 = new Sprite(image, 24, 24);
        s1.setPosition(0, 0);
        s1.setFrameSequence(disappear);
        vVortex.addElement(s1);

        Sprite s2 = new Sprite(image, 24, 24);
        s2.setPosition(0, 9 * 24);
        s2.setFrameSequence(disappear);
        vVortex.addElement(s2);

        Sprite s3 = new Sprite(image, 24, 24);
        s3.setPosition(9 * 24, 9 * 24);
        s3.setFrameSequence(disappear);
        vVortex.addElement(s3);

        Sprite s4 = new Sprite(image, 24, 24);
        s4.setPosition(9 * 24, 0);
        s4.setFrameSequence(disappear);
        vVortex.addElement(s4);    
    }
    
    public void setElement(int i, int set) {
        switch(i) {
            
            case 0:gameMng.element[0][0] = set;break;
            case 1:gameMng.element[0][9] = set;break;
            case 2:gameMng.element[9][9] = set;break;
            case 3:gameMng.element[9][0] = set;break;
        }
    }
    
    public void turn(long currTime) {        
        
        if(currTime - time > 30) {
            
            for(int i = 0; i < vVortex.size(); i++) {
                
                if(appearManager[i]) {
                    
                    ((Sprite)vVortex.elementAt(i)).prevFrame();
                    if(((Sprite)vVortex.elementAt(i)).getFrame() == 0)
                        ((Sprite)vVortex.elementAt(i)).setFrameSequence(turn);
                }
                else {
                    
                    ((Sprite)vVortex.elementAt(i)).nextFrame();                    
                    if(((Sprite)vVortex.elementAt(i)).getFrame() == 15)
                        ((Sprite)vVortex.elementAt(i)).setVisible(false);
                }
            }       
            
            time = currTime;
        }
    }
    
    public void appearManager() {
        
        for(int i = 0; i < 4; i++)
            appearManager[i] = true;
        
        int n = Math.abs(gameMng.r.nextInt() % 5);
        
        for(int i = 0; i < n; i++)
            appearManager[Math.abs(gameMng.r.nextInt() % 4)] = false;         
        
        for(int i = 0; i < 4; i++)
            if(appearManager[i]) {
                
                setElement(i, 1);
                ((Sprite)vVortex.elementAt(i)).setVisible(true);
            }
            else {
                
                setElement(i, 0);
                ((Sprite)vVortex.elementAt(i)).setFrameSequence(disappear);
            }
    }
}